package com.hangzhou.xyj.ui.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.model.DramaCategoryModel;
import com.hangzhou.xyj.model.DramaPageInfoModel;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.model.UserPageInfoModel;
import com.hangzhou.xyj.model.type.EBannerType;
import com.hangzhou.xyj.net.handler.GetBannerHandler;
import com.hangzhou.xyj.net.handler.GetListHandler;
import com.hangzhou.xyj.net.handler.GetUserByCatHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetBannerReq;
import com.hangzhou.xyj.net.request.GetListReq;
import com.hangzhou.xyj.net.request.GetUserByCatReq;
import com.hangzhou.xyj.ui.adapter.FamousExpertAdapter;
import com.hangzhou.xyj.ui.fragment.base.BaseFragment;

import java.util.List;

/**
 * 名家
 *
 * @author neo.duan
 */
public class FamousExpertFragment extends BaseFragment implements OnClickListener, FamousExpertAdapter.OnCategoryChangeListener {

    private RecyclerView mRecyclerView;
    private FamousExpertAdapter mAdapter;

    @Override
    public void initTop() {
        enableTop(false);
        enableRefresh(true);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.fragment_famous_expert);
    }

    @Override
    public void initViews() {
        setLoadingAnimVisible(true);//显示加载动画
        mRecyclerView = findView(R.id.rv_famous_expert);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new FamousExpertAdapter(mContext));
        mAdapter.setOnCategoryChangeListener(this);//设置改变剧种之后的回调监听
    }

    @Override
    public void initDatas() {
        getBanner();
    }

    @Override
    public void initListeners() {

    }

    /**
     * 获取banner条数据
     */
    private void getBanner() {
        GetBannerReq req = new GetBannerReq(EBannerType.STAGE);
        HttpLoader.getInstance().sendRequest(new GetBannerHandler(req, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                hideParentLoadingDialog();
//				super.onSuccess(model); 不需要关闭Loading对话框，所以注释掉
                //更新banner数据
                List<BannerModel> bannerList = (List<BannerModel>) model;
                mAdapter.updateBanner(bannerList);
                //获取banner成功，再获取剧种
                getCategory();
            }
        }, 0));
    }

    /**
     * 获取剧种信息
     */
    private void getCategory() {
        GetListReq req = new GetListReq();
        HttpLoader.getInstance().sendRequest(new GetListHandler(req, new MyHttpResponseListener() {

            @Override
            public void onStart() {
                //不要loading框
            }

            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model); //不需要关闭Loading对话框，所以注释掉
                DramaPageInfoModel infoModel = (DramaPageInfoModel) model;
                List<DramaCategoryModel> categoryList = infoModel.getDramaCategoryList();
                mAdapter.updateCategory(categoryList);
                categoryList.get(0).setIsSelected(true);//第一个剧种选中
                mAdapter.setCurrrentCategoryId(categoryList.get(0).getId());
                mAdapter.setCurrCatName(categoryList.get(0).getName() + "-名家");
                getUserByCat(categoryList.get(0).getId());//默认选中第一个剧种，所以传入的categoryId为第一个剧种的Id
            }
        }, 0));
    }

    /**
     * 获取剧种下的名家
     *
     * @param categoryId 剧种的Id
     * @return
     * @author yaohuix
     * @date 2016/3/9 14:21
     */
    private void getUserByCat(int categoryId) {
        GetUserByCatReq getUserByCatReq = new GetUserByCatReq(GetUserByCatReq.TYPE_FAMOUS_EXPERT, categoryId, 8, 1);//获取8条数据即可
        HttpLoader.getInstance().sendRequest(new GetUserByCatHandler(getUserByCatReq, new MyHttpResponseListener() {
            @Override
            public void onStart() {
                //不要loading框
            }

            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<UserModel> list = ((UserPageInfoModel) model).getUserList();
                mAdapter.updateCatUser(list);
                setLoadingAnimVisible(false);//隐藏
            }
        }, 0));
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 剧种更新之后，回调的接口
     *
     * @param categoryId 剧种Id
     * @return
     * @author yaohuix
     * @date 2016/3/9 16:00
     */
    @Override
    public void updateUserByCat(int categoryId) {
        getUserByCat(categoryId);
    }
}
